< previous page page_417 next page >

Page 417
Determining an Object's Type
In the previous example, I mentioned that if you pass a text box to the StateLoad() routine, you get an error because a text box doesnt have a Clear routine. Therefore, you should add a test to this procedure to see whether the right type of control was passed in. To accomplish this, you use the TypeOf operator. Lets rewrite the StateLoad() procedure you just wrote to make sure that only combo boxes and list boxes are used by this procedure:
9f31265bb40b592b1929e18a47ab28e5.gif
Public Sub StateLoad(ctlStates As Control)
   If TypeOf ctlStates Is ListBox Then
      ' Do Nothing
   ElseIf TypeOf ctlStates Is ComboBox Then
      ' Do Nothing
   Else
      Exit Sub
   End If
   With ctlStates
      .Clear

      .AddItem "Iowa"
      .AddItem "California"
      .AddItem "Arizona"
      .AddItem "Nevada"
      .AddItem "Arkansas"
      .AddItem "Washington"
      .AddItem "Texas"
      .AddItem "Nebraska"
      .AddItem "Utah"
      .AddItem "Kentucky"
   End With
End Sub
When to Use Generic Versus Specific Object Types
Although its tempting to use generic variables when passing arguments to procedures, there is a drawback to this. Visual Basic is forced to use late binding when a generic object variable is used. What this means is that Visual Basic has to wait until runtime to determine the type of object thats actually passed to the procedure. This is expensive in terms of performance. Visual Basic must resolve the reference to its internal table every time a method or property is accessed in this generic object.

 
< previous page page_417 next page >

If you like this book, buy it!